from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from tools.MyWidget import ClickLabel
from tools.User import LoginManager
from tools.User import User
from tools.MyWidget import HboxBarWidget
from tools.MyWidget import RegisterBar
import sys
import os
import json
import page1
import page3
import page5
import page0
import _thread

class LoginWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)
        self.forget_label = ClickLabel(self)
        self.forget_label.setText("忘记密码?")
        self.login_button = QPushButton("登录",self)
        self.user_line = QLineEdit(self)
        self.pass_line = QLineEdit(self)
        self.user_line.setPlaceholderText("用户名")
        self.pass_line.setPlaceholderText("密码")
        self.role_box = QComboBox(self)
        self.role_box.addItem("管理员")
        self.role_box.addItem("教师")
        self.role_box.addItem("教学秘书")
        self.reg_bar = RegisterBar()
        self.register_label = self.reg_bar.register_label
        self.init_ui()
        self.set_widget_style()
    def init_ui(self):
        self.setWindowTitle('用户登录')
        self.setGeometry(500,300,540,340)
        #修改背景色为白色
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,Qt.white)
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        #设置相关事件
        self.login_button.clicked.connect(self.login)
        self.forget_label.connect_customized_slot(self.forget_widow)
        self.register_label.connect_customized_slot(self.reg_widow)

    def set_widget_style(self):
        self.layout.setContentsMargins(20,0,20,20)
        self.layout.setSpacing(0)
        self.layout.addWidget(HboxBarWidget(40,40,[self.reg_bar]))
        self.layout.addWidget(HboxBarWidget(40,40,[self.user_line]))
        self.layout.addWidget(HboxBarWidget(40,40,[self.pass_line]))
        self.layout.addWidget(HboxBarWidget(35,35,[self.forget_label,self.role_box]))
        self.layout.addWidget(HboxBarWidget(35,35,[self.login_button]))
        login_font = QFont('Microsoft YaHei',22)
        login_font.setPixelSize(22)
        self.user_line.setFixedHeight(40)
        self.pass_line.setFixedHeight(40)
        self.role_box.setFixedWidth(110)
        self.login_button.setFixedHeight(35)
        login_btn_font = QFont('Microsoft YaHei',16)
        login_btn_font.setPixelSize(16)
        self.login_button.setFont(login_btn_font)
        self.login_button.setStyleSheet('background-color:#49a8f8;color:rgb(255,255,255);border-radius:8px')
        self.forget_label.setStyleSheet(
            'QLabel{color:#7f7f7f;}'
            'QLabel:hover{color:#92c9fb;}'
        )
        self.pass_line.setStyleSheet(
            "QLineEdit{border-color:#f0f0f0;}"
        )
        self.pass_line.setEchoMode(2)
        self.user_line.setStyleSheet(
            "QLineEdit{border-color:#f0f0f0;}"
        )
        self.register_label.setStyleSheet(
            'QLabel{color:#92c9fb;}'
            'QLabel:hover{color:#92c9fb;}'
        )
    def reg_widow(self):
        self.a = Register()
        self.a.show()
        self.close()
    def forget_widow(self):
        self.b = ForgetPass()
        self.b.show()
        self.close()
    def login(self):
        if not os.path.exists("tmp"):
            os.mkdir("tmp")
        if not os.path.exists("tmp/user"):
            with open("tmp/user","w") as f:
                f.write("")
        role_dict = {"管理员":1,"教学秘书":2,"教师":3}
        role_name = self.role_box.currentText()
        username = self.user_line.text()
        passwd = self.pass_line.text()
        login = LoginManager()
        rlt = json.loads(login.login(username,passwd,role_dict[role_name]))
        if not rlt["status"]:
            QMessageBox.warning(self,"登录失败",rlt["msg"],QMessageBox.Yes)
        else:
            user = User()
            user.session("tmp/user",username)
            if rlt["role"]==1:
                self.w = page0.Test()
            elif rlt["role"]==2:
                self.w = page5.Test()
            else:
                self.w = page1.ClassManager()
            self.close()
            self.w.show()
class Register(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('用户注册')
        self.setGeometry(500,300,500,520)
        self.reg_label = QLabel("注册",self)
        self.user_label = QLabel("已有账号",self)
        self.login_label = ClickLabel(self)
        self.login_label.setText("登录")
        self.register_button = QPushButton("注册",self)
        self.user_line = QLineEdit(self)
        self.pass_line = QLineEdit(self)
        self.confirm_line = QLineEdit(self)
        self.phone_line = QLineEdit(self)
        self.email_line = QLineEdit(self)
        self.college = QLineEdit(self)
        self.truename_line = QLineEdit(self)
        self.check = QCheckBox("教学秘书", self)
        self.manager = LoginManager()
        self.init_ui()
        self.set_widget_style()
    def init_ui(self):
        #设置点击事件
        self.register_button.clicked.connect(self.register)
        self.login_label.connect_customized_slot(self.login_widow)
        #修改背景色为白色
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,Qt.white)
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        #设置组件的移动
        self.reg_label.move(65,20)
        self.user_label.move(370,30)
        self.login_label.move(430,30)
        self.user_line.move(65,65)
        self.pass_line.move(65,130)
        self.confirm_line.move(65,195)
        self.phone_line.move(65,260)
        self.email_line.move(270,260)
        self.college.move(65,325)
        self.truename_line.move(270, 325)
        self.check.move(65, 390)
        self.register_button.move(65,455)

    def set_widget_style(self):
        font = QFont('Microsoft YaHei')
        font.setPixelSize(22)
        self.reg_label.setFont(font)
        self.user_line.setFixedSize(385,40)
        self.pass_line.setFixedSize(385,40)
        self.confirm_line.setFixedSize(385,40)
        self.phone_line.setFixedSize(180,40)
        self.register_button.setFixedSize(385,35)
        self.email_line.setFixedSize(180,40)
        self.college.setFixedSize(180,40)
        self.truename_line.setFixedSize(180,40)
        font2 = QFont('Microsoft YaHei')
        font2.setPixelSize(16)
        self.register_button.setFont(font2)
        self.register_button.setStyleSheet('background-color:#49a8f8;color:rgb(255,255,255);border-radius:8px')
        self.reg_label.setStyleSheet(
            'QLabel{color:#7f7f7f;}'
        )
        self.pass_line.setStyleSheet(
            "QLineEdit{border-color:#f0f0f0;}"
        )
        self.pass_line.setEchoMode(2)
        self.confirm_line.setEchoMode(2)
        self.user_line.setStyleSheet(
            "QLineEdit{border-color:#f0f0f0;}"
        )
        self.email_line.setStyleSheet(
            "QLineEdit{border-color:#f0f0f0;}"
        )
        self.truename_line.setStyleSheet(
            "QLineEdit{border-color:#f0f0f0;}"
        )
        self.user_line.setPlaceholderText("用户名")
        self.pass_line.setPlaceholderText("密码")
        self.confirm_line.setPlaceholderText("确认密码")
        self.phone_line.setPlaceholderText("手机号")
        self.email_line.setPlaceholderText("电子邮箱")
        self.college.setPlaceholderText("学院")
        self.truename_line.setPlaceholderText("真实姓名")
        self.login_label.setStyleSheet(
            'QLabel{color:#92c9fb;}'
            'QLabel:hover{color:#92c9fb;}'
        )
        self.user_label.setStyleSheet(
            'QLabel{color:#7f7f7f;}'
        )
    def login_widow(self):
        self.a = LoginWindow()
        self.close()
        self.a.show()
    def register(self):
        username = self.user_line.text()
        passwd = self.pass_line.text()
        confirm = self.confirm_line.text()
        truename = self.truename_line.text()
        email = self.email_line.text()
        phone = self.phone_line.text()
        msg = json.loads(self.manager.register(username,truename,passwd,confirm,phone,email))
        if not msg['status']:
            QMessageBox.information(self,'提示',msg['msg'],QMessageBox.Yes)
        else:
            QMessageBox.information(self,'提示','注册成功',QMessageBox.Yes)
            self.close()
            self.login_widow()

class ForgetPass(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('找回密码')
        self.setGeometry(500,300,500,450)
        self.maccode_button = QPushButton("获取验证码",self)
        self.forget_pass_label = QLabel("找回密码",self)
        self.no_account_label = QLabel("暂无账号",self)
        self.register_label = ClickLabel(self)
        self.register_label.setText("注册")
        self.maccode_line = QLineEdit(self)
        self.confirm_button = QPushButton("确定",self)
        self.user_line = QLineEdit(self)
        self.pass_line = QLineEdit(self)
        self.confirm_line = QLineEdit(self)
        self.email_line = QLineEdit(self)
        self.manager = LoginManager()
        self.init_ui()
        self.set_widget_style()
    def init_ui(self):
        #设置点击事件
        self.confirm_button.clicked.connect(self.change_pass)
        self.register_label.connect_customized_slot(self.reg_widow)
        self.maccode_button.clicked.connect(self.sendCodeMessage)
        #修改背景色为白色
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,Qt.white)
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        #设置组件移动
        self.forget_pass_label.move(65,20)
        self.no_account_label.move(370,30)
        self.register_label.move(430,30)
        self.user_line.move(65,65)
        self.pass_line.move(65,130)
        self.confirm_line.move(65,190)
        self.maccode_button.move(370,250)
        self.email_line.move(65,250)
        self.maccode_line.move(65,310)
        self.confirm_button.move(65,380)

    def set_widget_style(self):
        font = QFont('Microsoft YaHei')
        font.setPixelSize(22)
        self.forget_pass_label.setFont(font)
        self.user_line.setFixedHeight(40)
        self.user_line.setFixedWidth(385)
        self.pass_line.setFixedSize(385,40)
        self.confirm_line.setFixedSize(385,40)
        self.email_line.setFixedSize(315,40)
        self.confirm_button.setFixedSize(385,35)
        self.maccode_button.setFixedSize(100,40)
        self.maccode_line.setFixedSize(385,40)
        font2 = QFont('Microsoft YaHei')
        font2.setPixelSize(16)
        self.confirm_button.setFont(font2)
        self.confirm_button.setStyleSheet('background-color:#49a8f8;color:rgb(255,255,255);border-radius:8px')
        self.forget_pass_label.setStyleSheet(
            'QLabel{color:#7f7f7f;}'
        )
        self.pass_line.setStyleSheet(
            "QLineEdit{border-color:#f0f0f0;}"
        )
        self.pass_line.setEchoMode(2)
        self.confirm_line.setEchoMode(2)
        self.user_line.setStyleSheet(
            "QLineEdit{border-color:#f0f0f0;}"
        )
        self.user_line.setPlaceholderText("用户名/手机号")
        self.pass_line.setPlaceholderText("新密码")
        self.confirm_line.setPlaceholderText("确认密码")
        self.email_line.setPlaceholderText("电子邮箱")
        self.maccode_line.setPlaceholderText("验证码")
        self.register_label.setStyleSheet(
            'QLabel{color:#92c9fb;}'
            'QLabel:hover{color:#92c9fb;}'
        )
        self.no_account_label.setStyleSheet(
            'QLabel{color:#7f7f7f;}'
        )
        self.maccode_button.setStyleSheet(
            'QPushButton {font-size: 12px;color: #92c9fb;background-color: #ffffff;border-style:none}'
        )
    def reg_widow(self):
        self.a = Register()
        self.a.show()
        self.close()
    def change_pass(self):
        manger = self.manager
        username = self.user_line.text()
        passwd = self.pass_line.text()
        confirm_pass = self.confirm_line.text()
        email = self.email_line.text()
        maccode = self.maccode_line.text()
        msg = json.loads(manger.forgotPassword(username,passwd,confirm_pass,email,maccode))
        if not msg['status']:
            QMessageBox.information(self,'重置失败',msg['msg'],QMessageBox.Yes)
        else:
            QMessageBox.information(self,'重置成功',msg['msg'],QMessageBox.Yes)
            self.close()
            self.login = LoginWindow()
            self.login.show()
    def sendCodeMessage(self):
        _thread.start_new_thread(self.manager.sendCodeMessage,(self.email_line.text(),))
        QMessageBox.information(self,'提示','验证码发送成功!',QMessageBox.Yes)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = LoginWindow()
    window.show()
    sys.exit(app.exec_())
    